package id.co.shuba.spamobile.widget;

import java.io.File;
import java.util.List;

import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.model.StandardInfo;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class FileAdapter extends BaseAdapter {
	private final Activity context;
	private final File[] list;
	

	public FileAdapter(Activity context, File[] list) {		
		this.context = context;
		this.list = list;
	}	
	
	public class ViewHolder {
		TextView txtFile;
		TextView txtSize;
		ImageButton btnDelete;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;		
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.file_item, null);		
			ViewHolder holder = new ViewHolder();
			holder.txtFile = (TextView) rowView.findViewById(R.id.txtFile);
			holder.txtSize = (TextView) rowView.findViewById(R.id.txtSize);
			holder.btnDelete = (ImageButton) rowView.findViewById(R.id.btnDelete);
			rowView.setTag(holder);
		}

		ViewHolder h = (ViewHolder) rowView.getTag();
		File file = list[position];
		h.txtFile.setText(file.getName());
		h.txtSize.setText(String.valueOf(file.length()/1024f)+"KB");
		h.btnDelete.setFocusable(false);
		h.btnDelete.setTag(file);

		return rowView;
	}

	public int getCount() {
		return list.length;
	}

	public Object getItem(int arg0) {
		return list[arg0];
	}	

	public long getItemId(int arg0) {		
		return 0;
	}
	
	public int getPosition(String value) {
		int i = 0;
		for (File info : list) {
			if (info.getName().equals(value))
				return i;
			i++;
		}
		return -1;
	}
}
